

#import "BIDCommitTableViewController.h"
#import "BIDAppDelegate.h"

@interface BIDCommitTableViewController ()

@end

@implementation BIDCommitTableViewController


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //fetch all commits from core date with name = table.title
    BIDAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Commit"];
    NSError *error;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                              @"(developer = %@)",self.title];
    [request setPredicate:predicate];
    self.commits = [context executeFetchRequest:request error:&error];
    //NSLog(@"number of %@ commits: %d", self.title, [self.commits count]);
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.commits count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"disclosureCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    // Configure the cell...
    NSString *revision = [[NSString alloc] initWithFormat:@"Revision %@", [self.commits[indexPath.row] valueForKey:@"revision"]];
    cell.textLabel.text = revision;
    cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
    //NSLog(@"indexPath: %@, row: %d", indexPath, indexPath.row);
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    UITableViewController *destination = segue.destinationViewController;
    destination.title = [[sender valueForKey:@"textLabel"] valueForKey:@"text"];
    //destination.title = [self.developers[currentCellIndex] valueForKey:@"name"];
    //transfer data to destination
}
@end
